#!/bin/bash
# -*- encoding: utf8 -*-

from config.config import reviewfile
from BModel.globalfun import OrderCrontall

def AddDataInsert(db):
    print reviewfile
    fp = open(reviewfile,'r')
    lastid = fp.readline()
    fp.close()
    oldsql = "SELECT ID,ReviewID,Rating,OrderID FROM `bw_ctrip_review_cn_v2` WHERE ID >(%s) AND  Rating <= 4 "%(lastid)
    data = db.bmquery(oldsql,'ctripn')
    insertsql,stip,maxid= "","",0
    if True == data['status']:
        for v in data['data']:
            maxid = v['ID']
            insertsql = insertsql+stip +"(%s,'%s',1)"%(v['ReviewID'], OrderCrontall(v['OrderID']))
            stip = ","

    if insertsql:
        hotelsql = "INSERT INTO BW_Order_Review_List(`ReviewID`,`Order`,`SourceID`) VALUES%s"%(insertsql)
        db.bmquery(hotelsql,'order')

    if 0 < maxid:
        fp = open(reviewfile,'w+')
        fp.write(str(maxid))
        fp.close()


if __name__ == "__main__":


    hcoon = MySQLdb.connect(**savedb)
    hcur = hcoon.cursor(cursorclass=MySQLdb.cursors.DictCursor)

    coonn = MySQLdb.connect(**ctripN)
    curn = coonn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
    NewDataInsert(curn,coonn,hcur,hcoon)
    print "携程接口升级之后数据补充完成"

